package com.captainbank.tcl.tclapplication.ui.activity.test;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.PagerSnapHelper;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.captainbank.tcl.tclapplication.R;
import com.captainbank.tcl.tclapplication.model.Banner;
import com.captainbank.tcl.tclapplication.ui.activity.basic.BaseActivity;
import com.captainbank.tcl.tclapplication.ui.adapter.LoopBannerAdapter;
import com.captainbank.tcl.tclapplication.ui.adapter.TextAdapter;
import com.captainbank.tcl.tclapplication.utils.DateUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.wenjian.loopbanner.LoopBanner;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import butterknife.BindView;

/**
 * Created by TCL on 2019/4/3.
 * Describe:
 */
public class TempTestActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    private List<Banner> bannerList;
    private TextAdapter textAdapter;
    private LoopBannerAdapter loopBannerAdapter;
    private List<String> textList;
    private LoopBanner lb;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_temp_test;
    }

    @Override
    protected void initData() {
        textList = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            textList.add("我是数据：" + (i + 1));
        }

        bannerList = new ArrayList<>();
        Banner banner;
        banner = new Banner();
        banner.setImageUrl("http://www.wanandroid.com/blogimgs/00f83f1d-3c50-439f-b705-54a49fc3d90d.jpg");
        bannerList.add(banner);
//        banner = new Banner();
//        banner.setImageUrl("http://test-cloud-wlz-com.oss-cn-shanghai.aliyuncs.com/test/%E6%94%BF%E5%BA%9C%E5%85%A5%E4%BD%8F.JPG");
//        bannerList.add(banner);
//        banner = new Banner();
//        banner.setImageUrl("http://test-cloud-wlz-com.oss-cn-shanghai.aliyuncs.com/test/%E9%A1%B9%E7%9B%AE%E5%AF%B9%E6%8E%A5%E4%BC%9A.jpg");
//        bannerList.add(banner);
    }

    @Override
    protected void initView() {
        toolbar.setTitle(getString(R.string.title_temp_test));

        //设置Banner
        View headerBanner = getLayoutInflater().inflate(R.layout.header_banner, null);
        lb = headerBanner.findViewById(R.id.loop_banner);
        loopBannerAdapter = new LoopBannerAdapter(this, bannerList, R.layout.item_loop_banner);
        lb.setAdapter(loopBannerAdapter);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerview.setLayoutManager(layoutManager);
        textAdapter = new TextAdapter(R.layout.item_text, textList);
        recyclerview.setAdapter(textAdapter);
        textAdapter.addHeaderView(headerBanner);
    }

    @Override
    protected void setListener() {
        refreshLayout.setOnRefreshListener(refreshlayout -> {
            long time = System.currentTimeMillis();
            String timeStr = DateUtil.timeStamp2Date(time, null);
            List<String> newTextList = textAdapter.getData();
            newTextList.add(0, timeStr + "\n新增数据");
            if (textAdapter != null) {
                textAdapter.setNewData(newTextList);
            }
            refreshLayout.finishRefresh(500);
        });

        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                List<Banner> newBannerList = new ArrayList<>(bannerList);
                Banner banner = new Banner();
                int type = new Random().nextInt(5) + 1;
                switch (type) {
                    case 2:
                        banner.setImageUrl("http://www.wanandroid.com/blogimgs/fb0ea461-e00a-482b-814f-4faca5761427.png");
                        break;
                    case 3:
                        banner.setImageUrl("http://www.wanandroid.com/blogimgs/62c1bd68-b5f3-4a3c-a649-7ca8c7dfabe6.png");
                        break;
                    case 4:
                        banner.setImageUrl("http://www.wanandroid.com/blogimgs/00f83f1d-3c50-439f-b705-54a49fc3d90d.jpg");
                        break;
                    case 5:
                        banner.setImageUrl("http://www.wanandroid.com/blogimgs/90cf8c40-9489-4f9d-8936-02c9ebae31f0.png");
                        break;
                    case 1:
                    default:
                        banner.setImageUrl("http://www.wanandroid.com/blogimgs/ab17e8f9-6b79-450b-8079-0f2287eb6f0f.png");
                        break;
                }
                newBannerList.add(banner);
                loopBannerAdapter.setNewData(newBannerList);
                bannerList = newBannerList;
                refreshLayout.finishLoadMore(500);
            }
        });

    }
}
